package com.fangcang.utils;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Date;

public class LocalDateUtils {

	/**
	 * 获取当前时间分钟数的向下十位取整  如：14：52：06-----14：40：00
	 * @param now
	 * @return
	 */
	public static LocalDateTime getFromTime(LocalDateTime now) {
		int minute = now.getMinute();
		//向下取整到十分位 如：28->20 02->00
		int floorMinute = minute/10*10;
		
		LocalDateTime from = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), now.getHour(), floorMinute, 0);
		from = from.minusMinutes(10);
		return from;
	}
	
	/**
	 * 获取当前时间分钟十分位向上整-1s，如14：52：06-----14：49：59
	 * @param now
	 * @return
	 */
	public static LocalDateTime getEndTime(LocalDateTime now) {
		int minute = now.getMinute();
		//向下取整到十分位 如：28->20 02->00
		int floorMinute = minute/10*10;
		
		LocalDateTime from = LocalDateTime.of(now.getYear(), now.getMonth(), now.getDayOfMonth(), now.getHour(), floorMinute, 0);
		//from = from.plusMinutes(10);
//		LocalDateTime end = from.plusSeconds(599);
		return from;
	}
	
	/**
	 * 将当前LocalDateTime对象转换为long毫秒
	 * @param dateTime
	 * @return
	 */
	public static long getLongTime(LocalDateTime dateTime) {
		if(dateTime == null) {
			return 0l;
		}
		return dateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
	}
	
	 public static LocalDateTime date2LocalDateTime(Date date){
	        Instant instant = date.toInstant();//An instantaneous point on the time-line.(时间线上的一个瞬时点。)
	        ZoneId zoneId = ZoneId.systemDefault();//A time-zone ID, such as {@code Europe/Paris}.(时区)
	        LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
	        return localDateTime;
	 }
	 
	 public static Date localDateTime2Date( LocalDateTime localDateTime){
	        ZoneId zoneId = ZoneId.systemDefault();
	        ZonedDateTime zdt = localDateTime.atZone(zoneId);//Combines this date-time with a time-zone to create a  ZonedDateTime.
	        Date date = Date.from(zdt.toInstant());
	        return date;
	 }
}
